﻿using UnityEngine;
using System.Collections;
using Clojure;
using System.Xml;
public class Weapon :MonoBehaviour
{
    public float AttackSpeed;
    public float AttackCD;
    public float AttackRange;
    EffectGeneric AttackEffect;  
    Context WeaponContext;
    public Unit OwnerUnit;
 
    public float CurBfeAtkRoll;
    public bool IsAttacking;
    public float BeforeAttackRoll;
    public void Init(Unit unit, XmlWeapon xmlWeapon) 
    {
        this.WeaponContext = new Context(unit.Context);
        this.WeaponContext.Define("self-weapon", new ObjectNode(this));
        this.AttackSpeed = xmlWeapon.AttackSpeed;
        this.AttackCD = this.AttackSpeed;
        this.AttackRange = xmlWeapon.AttackRange;
        this.OwnerUnit = unit;
        this.AttackEffect = EffectManager.Instance.CreateEffect(xmlWeapon.EffectName, unit);
        this.BeforeAttackRoll = xmlWeapon.BeforeAttackRoll;
    }
    public void ExecAttack(Unit targetUnit) 
    {
        this.CurBfeAtkRoll = this.BeforeAttackRoll;
        this.IsAttacking = true;

        if (this.AttackEffect != null)
        {
            this.AttackEffect.DstUnit = targetUnit;
           
        }
    }
    void Update() 
    {
        if (this.IsAttacking) 
        {
            this.CurBfeAtkRoll -= Time.deltaTime;
            if (this.CurBfeAtkRoll <= 0) 
            {
                if (this.AttackEffect != null) 
                {
                    this.AttackEffect.Exec();
                }
                this.IsAttacking = false;
                this.CurBfeAtkRoll = 0;
            }
        }
        if (AttackEffect != null) 
        {
            AttackEffect.Update();
        }
    }

}
